<?php

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$urlParams = $generator->generateUrlParams();
$nameAttribute = $generator->getNameAttribute();

echo "<?php\n";
?>

use yii\helpers\Html;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use common\libs\Constants;
use mdm\admin\components\Helper;
<?= $generator->enablePjax ? 'use yii\widgets\Pjax;\\n' : '' ?>
use <?= $generator->indexWidgetType === 'grid' ? "kartik\\grid\\GridView" : "yii\\widgets\\ListView" ?>;
use <?= ltrim($generator->searchModelClass, '\\') ?>;

/* @var $this View */
<?= !empty($generator->searchModelClass) ? "/* @var \$searchModel " . StringHelper::basename(ltrim($generator->searchModelClass, '\\')) . " */\n" : '' ?>
/* @var $dataProvider ActiveDataProvider */

$this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>;
$this->params['breadcrumbs'][] = $this->title;

Modal::begin([
    'id'=>'operate-modal',
    'header'=>'<h4 class="modal-title"></h4>'
]);
Modal::end();
$createUrl = Url::toRoute('create');
$viewUrl = Url::toRoute('view');
$updateUrl = Url::toRoute('update');
$uploadUrl = Url::toRoute('upload');
$createTitle = Yii::t('app','Create <?= Inflector::camel2words(StringHelper::basename($generator->modelClass))?>');
$updateTitle = Yii::t('app','Update <?= Inflector::camel2words(StringHelper::basename($generator->modelClass))?>');
$viewTitle = Yii::t('app','View <?= Inflector::camel2words(StringHelper::basename($generator->modelClass))?>');
$uploadTitle = Yii::t('app','Upload <?= Inflector::camel2words(StringHelper::basename($generator->modelClass))?>');

$js = <<<JS
// 创建操作
$('#create').on('click', function () {
    $('.modal-dialog').removeClass('modal-lg');
    $('.modal-title').html('{$createTitle}');
    $.get('{$createUrl}',
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 详情信息
$('.btn-view').on('click', function () {
    $('.modal-dialog').addClass('modal-lg');
    $('.modal-title').html('{$viewTitle}');
    $.get('{$viewUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 详情信息
$('.btn-upload').on('click', function () {
    $('.modal-dialog').addClass('modal-lg');
    $('.modal-title').html('{$uploadTitle}');
    $.get('{$uploadUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
// 更新操作
$('.btn-update').on('click', function () {
    $('.modal-dialog').removeClass('modal-lg');
    $('.modal-title').html('{$updateTitle}');
    $.get('{$updateUrl}', { id: $(this).closest('tr').data('key') },
        function (data) {
            $('.modal-body').html(data);
        }
    );
});
JS;
$this->registerJs($js);

?>
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index">
<?= $generator->enablePjax ? "    <?php Pjax::begin(); ?>\n" : '' ?>
<?php if(!empty($generator->searchModelClass)): ?>
<?= "    <?php " . ($generator->indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?>
<?php endif; ?>
    <?php echo "<?php" ?> if(Helper::checkRoute('create')) { ?>
    <p>
        <?= "<?= " ?>Html::a(<?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'],
        [
            'class' => 'btn btn-success',
            'id' => 'create',
            'data-toggle' => 'modal',
            'data-target' => '#operate-modal'
        ]) ?>
    </p>
    <?php echo "<?php" ?> }?>

<?php if ($generator->indexWidgetType === 'grid'): ?>
    <?= "<?= " ?>GridView::widget([
        'dataProvider' => $dataProvider,
        <?= !empty($generator->searchModelClass) ? "'filterModel' => \$searchModel,\n        'columns' => [\n" : "'columns' => [\n"; ?>
            ['class' => 'yii\grid\SerialColumn'],
<?php
$count = 0;
if (($tableSchema = $generator->getTableSchema()) === false) {
    foreach ($generator->getColumnNames() as $name) {
        if (++$count < 6) {
            echo "            '" . $name . "',\n";
        } else {
            echo "            // '" . $name . "',\n";
        }
    }
} else {
    foreach ($tableSchema->columns as $column) {
        $format = $generator->generateColumnFormat($column);
        if (++$count < 6) {
            echo "            '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
        } else {
            echo "            // '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n";
        }
    }
}
?>
    [
        'class' => 'yii\grid\ActionColumn',
        'template' => Helper::filterActionColumn('{view}{upload}{update}{delete}'),
        //                'header' => '操作',
        'headerOptions' => [
            'width' => '100px',
        ],
        'buttons' => [
            'view' => function($url, $model, $key){
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                      'title' => '详情',
                      'class' => 'btn btn-default btn-view',
                      'data-toggle' => 'modal',
                      'data-target' => '#operate-modal',
                      ]);
              },

            'update' => function($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                           'title' => '编辑',
                           'class' => 'btn btn-default btn-update',
                           'data-toggle' => 'modal',
                           'data-target' => '#operate-modal',
                        ]);
            },
            'delete' => function($url, $model, $key) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                    'title' => '删除',
                    'class' => 'btn btn-default',
                    'data' => [
                        'confirm' => '确定要删除么?',
                        'method' => 'post',
                    ],
                ]);
            },
        ],
    ],
],
    ]); ?>
<?php else: ?>
    <?= "<?= " ?>ListView::widget([
        'dataProvider' => $dataProvider,
        'itemOptions' => ['class' => 'item'],
        'itemView' => function ($model, $key, $index, $widget) {
            return Html::a(Html::encode($model-><?= $nameAttribute ?>), ['view', <?= $urlParams ?>]);
        },
    ]) ?>
<?php endif; ?>
<?= $generator->enablePjax ? "    <?php Pjax::end(); ?>\n" : '' ?>
</div>